﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using AircraftBattleServer;
using System.Media;

namespace AircraftBattleClient
{
    /// <summary>
    /// Interaction logic for GameLounge.xaml
    /// </summary>
    public partial class GameLounge : Window
    {
        public string username;

        public struct player
        {
            public string name { set; get; }
            public int team { set; get; }
            public string ready { set; get; }
        }

        private Thread backgroundThread;
        private int craftID;
        private static GameEngineServiceClient clt = new GameEngineServiceClient();
        SoundPlayer sndHover = new SoundPlayer(Constants.SoundFolderRelativePath
        + "\\" + "HoverMouse.wav");
        SoundPlayer sndClick = new SoundPlayer(Constants.SoundFolderRelativePath
                + "\\" + "ClickButton.wav");

        public GameLounge(string username)
        {
            // initialize
            InitializeComponent();
            this.username = username;
            this.label1.Content = this.username;
            this.label2.Content = "W: " + clt.GetWinRecord(username) + " / L: " + clt.GetLoseRecord(username); 

            craftID = clt.GetAircraftID(this.username);

            clt.AddPlayer(this.username, craftID);

            ((DataGridTextColumn)this.dataGrid1.Columns[0]).Binding = new Binding("name");
            ((DataGridTextColumn)this.dataGrid1.Columns[1]).Binding = new Binding("team");
            ((DataGridTextColumn)this.dataGrid1.Columns[2]).Binding = new Binding("ready");

            foreach (User u in clt.GetMapData().userList)
            {
                this.dataGrid1.Items.Add(new player() { name = u.userName, team = u.team, ready = ((u.ready==0)?"not ready":"ready") });
            }

            BGWorker2 threadWork = new BGWorker2(this);
            Thread newThread =
                new Thread(new ThreadStart(threadWork.Run));
            this.backgroundThread = newThread;
            this.backgroundThread.Start();
        }

        public void CloseGameLounge()
        {
            MainWindow mainWin = new MainWindow(this.username);
            mainWin.Show();
            this.backgroundThread.Abort();
            clt.StartGame();
            this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            clt.DeletePlayer(username, craftID);
            this.backgroundThread.Abort();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            sndClick.Play();
            clt.SetTeam(username, 1);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            sndClick.Play();
            clt.SetTeam(username, 2);
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            sndClick.Play();
            if (this.button3.Content.Equals("Ready"))
            {
                clt.SetReady(username, 1);
                this.button3.Content = "Unready";
            }
            else
            {
                clt.SetReady(username, 0);
                this.button3.Content = "Ready";
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            sndClick.Play();
            clt.DeletePlayer(username, craftID);
            Login log = new Login();
            log.Show();
            this.backgroundThread.Abort();
            this.Close();
        }

        private void button1_MouseEnter(object sender, MouseEventArgs e)
        {
            sndHover.Play();
        }

        private void button2_MouseEnter(object sender, MouseEventArgs e)
        {
            sndHover.Play();
        }

        private void button3_MouseEnter(object sender, MouseEventArgs e)
        {
            sndHover.Play();
        }

        private void button4_MouseEnter(object sender, MouseEventArgs e)
        {
            sndHover.Play();
        }
    }

    public class BGWorker2
    {
        GameLounge gameLounge;
        static GameEngineServiceClient clt = new GameEngineServiceClient();

        public BGWorker2(GameLounge lounge)
        {
            this.gameLounge = lounge;
        }

        public void Run()
        {
            while (true)
            {
                // Background thread can only access the UI thread via
                // the UI thread's dispatcher
                this.gameLounge.Dispatcher.Invoke(DispatcherPriority.Normal,
                    new Action(() => { UpdateGrid(); }));
                Thread.Sleep(200);
            }
        }

        public void UpdateGrid()
        {
            int flag = 1;
            int team1Flag = 1;
            int team2Flag = 1;
            
            this.gameLounge.dataGrid1.Items.Clear();
            foreach (User u in clt.GetMapData().userList)
            {
                this.gameLounge.dataGrid1.Items.Add(new GameLounge.player() { name = u.userName, team = u.team, ready = ((u.ready == 0) ? "not ready" : "ready") });
            }

            foreach (User u in clt.GetMapData().userList)
            {
                if (u.ready == 0) flag = 0;
            }

            foreach (User u in clt.GetMapData().userList)
            {
                if (u.team == 1) team1Flag = 0;
            }

            foreach (User u in clt.GetMapData().userList)
            {
                if (u.team == 2) team2Flag = 0;
            }

            if (flag == 1 && team1Flag == 0 && team2Flag == 0)
            {
                gameLounge.CloseGameLounge();
            }
        }
    }
}
